using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication4
{
    public partial class Map2 : Form
    {
        public Map2()
        {
            InitializeComponent();
        }

        public int zoom=5;
        public int spaceBetweenVillages = 1;
        Point offset = new Point(512, 763);
        World world;


        Bitmap bmp = null;

        public Map2(World world)
        {
            this.world = world;
            InitializeComponent();

            this.Text = this.Text + " - " + world.Name;
        }


        public int WorldWidth
        {
            get
            {
                return ClientRectangle.Width / (zoom+spaceBetweenVillages);
            }
        }
        public int WorldHeight
        {
            get
            {
                return ClientRectangle.Height / (zoom+spaceBetweenVillages);
            }
        }


    
        private void Map_Load(object sender, EventArgs e)
        {
            RedrawMap();
        }


        private void CreateMap() {

            this.SuspendLayout();
            List<VillageLabel> villagesToRemove = new List<VillageLabel>(1000);
            foreach(Control ctr in this.Controls){
                if (ctr is VillageLabel)
                {
                    villagesToRemove.Add((VillageLabel)ctr);
                }
            }
            foreach (VillageLabel vill in villagesToRemove)
            {
                this.Controls.Remove(vill);
            }

            if (world != null)
            {
                int zoomWidth = this.WorldWidth;
                int zoomHeight = this.WorldHeight;

                string select = "(X >= " + offset.X + " and X < " + (offset.X + zoomWidth  ) + ")"
                    + "AND (Y >= " + offset.Y + " and Y < " + (offset.Y + zoomHeight ) + ")";
                DataRow[] drs = world.dtVillages.Select(select);

                int vilX, vilY, mapX, mapY;
                VillageLabel lblVill;
                foreach (DataRow dr in drs)
                {
                    lblVill = new VillageLabel();
                    vilX = Convert.ToInt32(dr[World.CONSTS.VillagesTable.Columns.XIndex]);
                    vilY = Convert.ToInt32(dr[World.CONSTS.VillagesTable.Columns.YIndex]);
                    mapX = (vilX - offset.X ) * (zoom+1);
                    mapY = (vilY - offset.Y ) * (zoom+1);

                    if (dr[1].ToString().StartsWith("Bubaland") || dr[1].ToString().StartsWith("bubaland"))
                    {
                        lblVill.BackColor = Color.Yellow;
                        lblVill.Location = new System.Drawing.Point(mapX, mapY);
                        lblVill.Size = new System.Drawing.Size(zoom, zoom);
                    }
                    else if (dr[4].ToString() =="0")
                    {
                        lblVill.BackColor = Color.DarkGray;
                        lblVill.Location = new System.Drawing.Point(mapX, mapY);
                        lblVill.Size = new System.Drawing.Size(zoom, zoom);
                    }
                    else
                    {
                        lblVill.BackColor = Color.Blue;
                        lblVill.Location = new System.Drawing.Point(mapX, mapY);
                        lblVill.Size = new System.Drawing.Size(zoom, zoom);
                    }
                    this.Controls.Add(lblVill);
                }
            }

            this.ResumeLayout();

        }


        public void RedrawMap()
        {
            CreateMap();
        }

  
        //private void Map_ResizeEnd(object sender, EventArgs e)
        //{
        //    RedrawMap();
        //}

        class VillageLabel : Label
        {
        }

    }
}